package com.aaa.controller;

import com.aaa.entity.Pager;
import com.aaa.service.MenuService;
import com.aaa.service.MenuServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "MenuServlet",urlPatterns = "/menu.action")
public class MenuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null)
        {
            switch (method)
            {
                case "query":
                    query(request,response);
                    break;
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNo = request.getParameter("pageNo")==null?"1":request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize")==null?"1":request.getParameter("pageNo");


        Map<String,String> condition=new HashMap<>();
        condition.put("menu_name", request.getParameter("menu_name"));  //
        condition.put("parent_id", request.getParameter("parent_id"));
        MenuService service=new MenuServiceImpl();
        Pager pager=service.query(Integer.valueOf(pageNo), Integer.valueOf(pageSize), condition);
        response.getWriter().print(JSON.toJSONString(pager));

    }
}
